package com.airikeji.hr.job;

import com.airikeji.hr.domain.MessageResend;
import com.airikeji.hr.rabbitmq.RabbitMQSendUtil;
import com.airikeji.hr.service.MessageAlertService;
import com.airikeji.hr.service.MessageResendService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author huangrui
 * @Date 2021/7/16 10:52
 * @Version 1.0
 */
@Component
@Slf4j
public class TimingTask {

    @Autowired
    private MessageAlertService messageAlertService;
    @Autowired
    private MessageResendService messageResendService;
    @Autowired
    private RabbitMQSendUtil rabbitMQSendUtil;

    /**
     * 定时重新从message_resend表中取出，调用resend发送
     * 每5分钟执行一次
     */
    @Scheduled(cron = "* 0/5 * * * ?")
    public void resend(){
        log.info("开始消息定时重发机制！");
        List<MessageResend> messageResends = messageResendService.queryAll(); // 查出所有需要重发的消息
        for (int i = 0, len = messageResends.size(); i < len; i++) {
            MessageResend messageResend = messageResends.get(i);
            rabbitMQSendUtil.resend(messageResend);
        }
    }

    /*
     * 保底策略
     * 定时从message_alert表中取出数据提醒用户消息发送失败
     * 每1小时执行一次
     */
    @Scheduled(cron = "* 0 0/1 * * ?")
    public void remindMessage(){

    }

}
